Python 3 Object-Oriented Programming, Third Edition by Dusty Phillips
Author:Dusty Phillips
Language: eng
Format: epub, mobi
Tags: COM051000 - COMPUTERS / Programming / General, COM051360 - COMPUTERS / Programming Languages / Python, COM051210 - COMPUTERS / Programming / Object Oriented
Publisher: Packt Publishing
Published: 2018-11-14T07:33:16+00:00
def one(timer):
format_time("Called One")
def two(timer):
format_time("Called Two")
def three(timer):
format_time("Called Three")
class Repeater:
def __init__(self):
self.count = 0
def repeater(self, timer):
format_time(f"repeat {self.count}")
self.count += 1
timer.call_after(5, self.repeater)
timer = Timer()
timer.call_after(1, one)
timer.call_after(2, one)
timer.call_after(2, two)
timer.call_after(4, two)
timer.call_after(3, three)
timer.call_after(6, three)
repeater = Repeater()
timer.call_after(5, repeater.repeater)
format_time("Starting")
timer.run()
This example allows us to see how multiple callbacks interact with the timer. The first function is the format_time function. It uses the format string syntax to add the current time to the message; we'll read about them in the next chapter. Next, we create three simple callback methods that simply output the current time and a short message telling us which callback has been fired.
The Repeater class demonstrates that methods can be used as callbacks too, since they are really just functions that happen to be bound to an object. It also shows why the timer argument to the callback functions is useful: we can add a new timed event to the timer from inside a presently running callback. We then create a timer and add several events to it that are called after different amounts of time. Finally, we start the timer running; the output shows that events are run in the expected order:
02:53:35: Starting 02:53:36: Called One 02:53:37: Called One 02:53:37: Called Two 02:53:38: Called Three 02:53:39: Called Two 02:53:40: repeat 0 02:53:41: Called Three 02:53:45: repeat 1 02:53:50: repeat 2 02:53:55: repeat 3 02:54:00: repeat 4
Python 3.4 introduced a generic event loop architecture similar to this. We'll be discussing it later, in Chapter 13, Concurrency.
Download
Python 3 Object-Oriented Programming, Third Edition by Dusty Phillips.mobi
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12585)
Hello! Python by Anthony Briggs(9920)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9799)
The Mikado Method by Ola Ellnestam Daniel Brolund(9782)
Dependency Injection in .NET by Mark Seemann(9343)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(9299)
Hit Refresh by Satya Nadella(8826)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8305)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7786)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7768)
Grails in Action by Glen Smith Peter Ledbrook(7700)
The Kubernetes Operator Framework Book by Michael Dame(7666)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7562)
Exploring Deepfakes by Bryan Lyon and Matt Tora(7455)
Practical Computer Architecture with Python and ARM by Alan Clements(7378)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(7361)
Robo-Advisor with Python by Aki Ranin(7335)
Building Low Latency Applications with C++ by Sourav Ghosh(7242)
Svelte with Test-Driven Development by Daniel Irvine(7207)
